﻿using Station.Inspect;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Vision.Common
{
    /// <summary>
    /// 日志类
    /// </summary>
    public sealed class Log
    {
        #region 事件
        public event EventHandler<LogMsgArgs> OnLog;  //更新日志
        #endregion
        private static readonly Log instance = new Log() { Loglevel = logLevel.Debug };

        public logLevel Loglevel { get; set; }
        public static Log Instance
        {
            get { return instance; }
        }
        
        private string logPath = StaInspect.Instance.loadPath+"\\log";
        private string fileName;
        private bool isCreateByMonth = true;

        private string curPath = StaInspect.Instance.loadPath + "\\log";
        private int curYear;
        private int curMonth;
        private int curDay;

        private Object obj = new object();
        public void Init(string path, string fileName, bool isCreateByMonth)
        {
            this.logPath = path;
            this.fileName = fileName;
            this.isCreateByMonth = isCreateByMonth;
            curPath = logPath;
            CheckPath(DateTime.Now);
        }

        private void CheckPath(DateTime dt)
        {
            if (isCreateByMonth)
            {
                if (curYear != dt.Year || curMonth != dt.Month)
                {
                    string filePath = Path.Combine(logPath, dt.ToString("yyyy\\\\MM"));
                    Directory.CreateDirectory(filePath);
                    curYear = dt.Year;
                    curMonth = dt.Month;
                    curPath = filePath;
                }
            }
        }

        public bool Wirte(string message)
        {
            try
            {
                DateTime dt = DateTime.Now;
                //StringBuilder sb = new StringBuilder();
                //sb.AppendLine(dt.ToString("yyyy-MM-dd HH:mm:ss: ") + message);
                //File.AppendAllText($"{curPath}\\{dt.ToString("yyyy-MM-dd")}.txt", sb.ToString());
                lock (obj)
                {
                    CheckPath(dt);
                    File.AppendAllText($"{curPath}\\{dt.ToString("yyyy-MM-dd")}.txt", dt.ToString("yyyy-MM-dd HH:mm:ss: ") + message + "\r\n");
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool Debug(String format, params object[] args)
        {
            if (string.IsNullOrEmpty(format))
                return false;
            string str = args.Length > 0 ? string.Format(format, args) : format;
            Wirte("Debug:" + str);
            //OnLog(this, new LogMsgArgs(logLevel.Debug, str));
            return true;
        }

        public bool Info(String format, params object[] args)
        {
            if (string.IsNullOrEmpty(format))
                return false;
            string str = args.Length > 0 ? string.Format(format, args) : format;
            Wirte("Info:" + str);
            OnLog?.Invoke(this, new LogMsgArgs(logLevel.Info, str));
            return true;
        }

        public bool Warning(String format, params object[] args)
        {
            if (string.IsNullOrEmpty(format))
                return false;
            string str = args.Length > 0 ? string.Format(format, args) : format;
            Wirte("Warning:" + str);
            OnLog?.Invoke(this, new LogMsgArgs(logLevel.Warning, str));
            return true;
        }

        public bool Error(String format, params object[] args)
        {
            try
            {
                if (string.IsNullOrEmpty(format))
                    return false;
                string str = args.Length > 0 ? string.Format(format, args) : format;
                Wirte("Error:" + str);
                OnLog?.Invoke(this, new LogMsgArgs(logLevel.Error, format));
            }
            catch (Exception ex)
            {

            }
            return true;
        }
    }
    /// <summary>
    /// 日志等级
    /// </summary>
    public enum logLevel
    {
        Debug,
        Info,
        Warning,
        Error
    }

    /// <summary>
    /// 日志事件
    /// </summary>
    public class LogMsgArgs : EventArgs
    {
        public logLevel level;
        public string message;
        public LogMsgArgs(logLevel level, string message)
        {
            this.level = level;
            this.message = message;
        }
    }
}
